package com.example.mybus;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.mybus.model.Micro;
import com.example.mybus.model.pmf.PMF;

public class CrearMicroServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger("CrearMicroServlet");
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
//		Micro m1 = new Micro();
//		m1.setPatente("AAA000");
//		m1.setButacas(new Integer(30));
//		
//		Micro m2 = new Micro();
//		m2.setPatente("AAA001");
//		m2.setButacas(new Integer(50));
//		
//		Micro m3 = new Micro();
//		m3.setPatente("AAA002");
//		m3.setButacas(new Integer(56));
		
		String patente = req.getParameter("patente");
		String butacas = req.getParameter("butacas");
		
		Micro micro = new Micro();
		micro.setButacas(Integer.valueOf(butacas));
		micro.setPatente(patente);
		
		
		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			pm.makePersistent(micro);
			log.log(Level.INFO, "micro creado");
			
		} finally {
			pm.close();
		}
//		resp.getWriter().write("Micros creados!");
		req.getRequestDispatcher("micros").forward(req, resp);
	}
}
